package com.ws.universal.tools.adapter;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.ws.universal.tools.utils.LogcatUtils;

import java.util.List;

/**
 * 相对于页面较少 而且固定数量的时候 调用
 */
public class LiveViewPagerFragmentPagerAdapter extends FragmentPagerAdapter {

    public final List<Fragment> mFragmentList;

    public LiveViewPagerFragmentPagerAdapter(@NonNull FragmentManager fm, List<Fragment> mFragmentList) {
        super(fm);
        this.mFragmentList = mFragmentList;
    }


    @NonNull
    @Override
    public Fragment getItem(int position) {
        // 新建一个Fragment来展示ViewPager item的内容，并传递参数
        Fragment fragment = mFragmentList.get(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {
        return view == ((Fragment) obj).getView();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void finishUpdate(@NonNull ViewGroup container) {
        try {
            super.finishUpdate(container);
        } catch (NullPointerException nullPointerException) {
            LogcatUtils.e("", "Catch the NullPointerException in FragmentPagerAdapter.finishUpdate");
        }

    }
}
